![]() |
PATH![]() |
![]() ![]() |
This section describes how you coerce values from one class to another. For specific information on coercing Unit Type values, see Unit Type Value Classes. For information on coercing Unicode Text and International Text values, see Unicode Text and International Text. For information on coercing additional classes, see Other Value Classes.
Coercing is the process of converting a value from one class to another. AppleScript coerces values in one of two ways:
The ability to coerce a value from one class to another is either a built-in function of AppleScript or a capability provided by a scripting addition command. All of the coercions described in this section are built into AppleScript, so you can use them in any script and they do not need to be enclosed in a Tell block.
The As operator specifies a particular coercion. You can use the As operator to coerce a value to the correct class before using it as a command parameter or operand. For example, the following statement coerces the integer 2 into the string "2" before storing it in the variable myString :
set myString to 2 as string
Similarly, this statement coerces the string "2" to the integer 2 , so that it can be added to the other operand, 8 :
"2" as integer + 8
If you provide a command parameter or operand of the wrong class, AppleScript automatically coerces the operand or parameter to the expected class, if possible. For example, when AppleScript executes the following repeat statement, it expects an integer for the number of times to repeat the enclosed display dialog command (a scripting addition command).
repeat "2" times
display dialog "Hello"
end repeat
If you pass a string, as in this example, AppleScript attempts to coerce the string to an integer. If you pass a string that AppleScript can't coerce to an integer, such as "many", it reports an error.
Not all values can be coerced to all other classes of values. Figure 3-3 summarizes the coercions that AppleScript supports for commonly used value classes. To use the figure, find the class of the value to be coerced in the column at the left. Search across the table to the column labeled with the class to which you want to coerce the value. If there is a square at the intersection, then AppleScript supports the coercion.
Reference values are not included in the table because the contents of the reference determine whether the value specified by a reference can be coerced to a desired class.
For more information about each coercion, see the corresponding value class definitions in this chapter.
Note
When coercing strings to values of class Integer, Number, or Real or vice versa, AppleScript uses the current settings in the Numbers control panel for decimal and thousands to determine what separators to use in the string.
When coercing strings to values of class Date or vice versa, AppleScript uses the current settings in the Date & Time control panel for date and time format.
Figure 3-3 Coercions supported by AppleScript
Three of the identifiers mentioned at the top of Figure 3-3 act only as synonyms for other value classes: "Number" is a synonym for either "Integer" or "Real," "Text" is a synonym for "String," and "Styled Text" is a synonym for a string that contains style and font information. You can coerce values using these synonyms, but the class of the resulting value is always the appropriate value class, not the synonym. Here are some examples:
set x to 1.5 as number
class of x
--result: real
set x to 4 as number
class of x
--result: integer
set x to "Hello" as text
class of x
--result: string